#===============================================================================
# Koro Business Environment
# Copyright (C) 2007,2008,2010  Antonio Corroppoli
#
#This file is part of Koro BE.
# 
# Koro BE is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# Koro BE is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Koro BE; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#===============================================================================

'''
Created on 23/dic/2010

@author: antstud
'''

import shelve as persister
#import os
import os.path
#import zlib
from koro import ksettings

def _open(*args,**kwargs):
    return persister.open(*args,**kwargs)

class ObjectPersister(object):
    _NAME = ksettings.KOROBE_CFG
    _PATH = ksettings.KOROBE_DIR
    #os.path.normpath(os.path.join(os.path.normpath(USER_HOME),os.path.normpath(_NAME)))
    
    @staticmethod
    def use(db_name):
        ObjectPersister._NAME=db_name
        ObjectPersister.set_path()
        if not os.path.exists(ObjectPersister._PATH):
            db=_open(ObjectPersister._PATH,'c',protocol=2)
            db.close()
    
    @staticmethod
    def set_path():
        ObjectPersister._PATH = os.path.normpath(
                                os.path.join(os.path.normpath(ksettings.KOROBE_DIR),
                                             os.path.normpath(ObjectPersister._NAME)))
    
    @staticmethod
    def put(key,data):
        print 'ObjectPersister.put'
        tpath= ObjectPersister._PATH
        if not os.path.isabs(tpath):
            raise
        db=_open(tpath,'c',protocol=2)
        key=str(key)
        db[key]=data
        db.close()
        
    @staticmethod    
    def remove(key):
        if not os.path.isabs(ObjectPersister._PATH):
            raise
        db=_open(ObjectPersister._PATH,'w',protocol=2)
        key=str(key)
        if key in db:
            del db[key]
        db.close()
        
    @staticmethod    
    def clear():
        if not os.path.isabs(ObjectPersister._PATH):
            raise
        db=_open(ObjectPersister._PATH,'w',protocol=2)
        db.clear()
        db.close()
    
    @staticmethod    
    def get(key):
        if not os.path.isabs(ObjectPersister._PATH):
            raise
        db=_open(ObjectPersister._PATH,'r',protocol=2)
        key=str(key)
        hit=None
        if key in db:
            hit=db[key]
        db.close()
        return hit
    
    @staticmethod
    def all():        
        if not os.path.isabs(ObjectPersister._PATH):
            raise
        hit={}
        db=_open(ObjectPersister._PATH,'r',protocol=2)
        for key in db:
            hit[key]=db[key]
        db.close()
        return hit
